﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using TableTop.Presentation.Controls;
using TableTop.DataManagement.Addon;
using TableTop.DataManagement;

namespace TableTop.Presentation.Pages
{
    /// <summary>
    /// Interaction logic for CharacterSheetEditorPage.xaml
    /// </summary>
    public partial class CharacterSheetEditorPage : Page
    {
        public CharacterSheetEditorPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(CharacterSheetEditorPage_Loaded);
        }

        private void CharacterSheetEditorPage_Loaded(object sender, RoutedEventArgs e)
        {
            AddonInfo[] Addons = null;
            Addons = DataHelper.Addons.Addons;
            this.cmbAddons.ItemsSource = Addons;
        }

        private void btnCreateNew_Click(object sender, RoutedEventArgs e)
        {
            AddonInfo aiInfo = null;
            Addon SelectedAddon = null;
            CharacterSheetEditor csWriter = null;
            
            if (this.cmbAddons.SelectedItem != null)
            {
                aiInfo = (AddonInfo)this.cmbAddons.SelectedItem;
                SelectedAddon = DataHelper.Addons[aiInfo.GUID];
                csWriter = (CharacterSheetEditor) SelectedAddon.EntryPoint.CreateAddonTypeInstance(TableTopControls.CharacterSheetEditor);
                csWriter.SaveSheet += new EventHandler<SaveCharacterSheetEventArgs>(csWriter_SaveSheet);
                // this.SheetsPanel.Children.Add(csWriter);
                this.SheetsPanel.Content = csWriter;
            }
        }

        private void btnCloseCurrent_Click(object sender, RoutedEventArgs e)
        {
            if (this.SheetsPanel.Content != null)
                this.SheetsPanel.Content = null;
        }

        private void csWriter_SaveSheet(object sender, SaveCharacterSheetEventArgs e)
        {
            DataHelper.DataStorage.Store(e.CharacterSheet);
            // MessageBox.Show("Save request. Data: " + e.CharacterSheet.ToString());
        }        
    }
}
